set more off

use dataset, clear

*** rescale change in tax bill ***

replace dtaxbill = dtaxbill/l.medianincome*100

*** covariates ***

local controls dfederalaidpcp dstateaidpcp lpopulation pct_white pct_senior pct_renter lmedianincome 
local economy davgannualwage demployment dunemployment propvaluechange

*** make interactions ***

foreach x in dtaxbill davg demp propv dunemp {
	gen demincumbentX`x'=`x'*demincumbent
	su demincumbentX`x', det
	replace demincumbentX`x' = . if demincumbentX`x' > r(p99) | demincumbentX`x' < r(p1)
	su demincumbentX`x'
	replace demincumbentX`x' = (demincumbentX`x' - r(min))/(r(max)-r(min))
	gen demincumbentX`x'Xoverridep = demincumbentX`x'*overridep
}

local economypropval davgannualwage demployment dunemployment
local economydavg demployment dunemployment propvaluechange
local economydemp davgannualwage dunemployment propvaluechange
local economyemp davgannualwage dunemployment propvaluechange

eststo clear

gen lecon = demincumbentXdtaxbill
gen intxn = demincumbentXdtaxbillXoverridep
eststo: areg demvoteshare overridep lecon intxn lagdemvoteshare [aw=totalvotes], a(year) cl(town)
lincom overridep + intxn
local myp = 2*(1-normal(abs(`r(estimate)'/`r(se)')))
local mystars ""
if `myp' < 0.05 local mystars = "`mystars'" + "*" 
if `myp' < 0.01 local mystars = "`mystars'" + "*" 
if `myp' < 0.001 local mystars = "`mystars'" + "*" 
estadd local myest = string(r(estimate), "%20.02fc") + "`mystars'"
estadd local myse = "(" + string(r(se), "%20.02fc") + ")"
estadd local controls "No"

eststo: areg demvoteshare overridep lecon intxn lagdemvoteshare `economy' `controls' [aw=totalvotes], a(year) cl(town)
lincom overridep + intxn
local myp = 2*(1-normal(abs(`r(estimate)'/`r(se)')))
local mystars ""
if `myp' < 0.05 local mystars = "`mystars'" + "*" 
if `myp' < 0.01 local mystars = "`mystars'" + "*" 
if `myp' < 0.001 local mystars = "`mystars'" + "*" 
estadd local myest = string(r(estimate), "%20.02fc") + "`mystars'"
estadd local myse = "(" + string(r(se), "%20.02fc") + ")"
estadd local controls = "Yes"

drop lecon intxn
gen lecon = demincumbentXdavg
gen intxn = demincumbentXdavgX
eststo: areg demvoteshare overridep lecon intxn lagdemvoteshare [aw=totalvotes], a(year) cl(town)
lincom overridep + intxn
local myp = 2*(1-normal(abs(`r(estimate)'/`r(se)')))
local mystars ""
if `myp' < 0.05 local mystars = "`mystars'" + "*" 
if `myp' < 0.01 local mystars = "`mystars'" + "*" 
if `myp' < 0.001 local mystars = "`mystars'" + "*" 
estadd local myest = string(r(estimate), "%20.02fc") + "`mystars'"
estadd local myse = "(" + string(r(se), "%20.02fc") + ")"
estadd local controls = "No"

eststo: areg demvoteshare overridep lecon intxn lagdemvoteshare `economydavg' `controls' [aw=totalvotes], a(year) cl(town)
lincom overridep + intxn
local myp = 2*(1-normal(abs(`r(estimate)'/`r(se)')))
local mystars ""
if `myp' < 0.05 local mystars = "`mystars'" + "*" 
if `myp' < 0.01 local mystars = "`mystars'" + "*" 
if `myp' < 0.001 local mystars = "`mystars'" + "*" 
estadd local myest = string(r(estimate), "%20.02fc") + "`mystars'"
estadd local myse = "(" + string(r(se), "%20.02fc") + ")"
estadd local controls = "Yes"

drop lecon intxn
gen lecon = demincumbentXdemp
gen intxn = demincumbentXdempX
eststo: areg demvoteshare overridep lecon intxn lagdemvoteshare [aw=totalvotes], a(year) cl(town)
lincom overridep + intxn
local myp = 2*(1-normal(abs(`r(estimate)'/`r(se)')))
local mystars ""
if `myp' < 0.05 local mystars = "`mystars'" + "*" 
if `myp' < 0.01 local mystars = "`mystars'" + "*" 
if `myp' < 0.001 local mystars = "`mystars'" + "*" 
estadd local myest = string(r(estimate), "%20.02fc") + "`mystars'"
estadd local myse = "(" + string(r(se), "%20.02fc") + ")"
estadd local controls = "No"

eststo: areg demvoteshare overridep lecon intxn lagdemvoteshare `economydemp' `controls' [aw=totalvotes], a(year) cl(town)
lincom overridep + intxn
local myp = 2*(1-normal(abs(`r(estimate)'/`r(se)')))
local mystars ""
if `myp' < 0.05 local mystars = "`mystars'" + "*" 
if `myp' < 0.01 local mystars = "`mystars'" + "*" 
if `myp' < 0.001 local mystars = "`mystars'" + "*" 
estadd local myest = string(r(estimate), "%20.02fc") + "`mystars'"
estadd local myse = "(" + string(r(se), "%20.02fc") + ")"
estadd local controls = "Yes"

label variable overridep "(1) Tax increase"
label variable lecon "(2) Moderator"
label variable intxn "(3) Increase*Moderator"

#delimit;

esttab
	using "table 4.tex"
	,
		replace
		cells(b(fmt(2) star) se(par))
		eqlabels(none)
		nomtitles
		collabels(none)
		keep(overridepassed lecon intxn)
		gap
		label
		mlabels(none)
		nonotes
		booktabs
		width(\hsize)
		stats(
			myest
			myse
			controls
			N
			
			,
				label(
					"(1) + (3)"
					" "
					"Controls"
					"Observations"
					
						
				)
				fmt(0 0 0 %20.0fc)
		)
		mgroups(
			"Tax Bills" "Wages" "Employment"
			,
				pattern(1 0 1 0 1 0)
				prefix(\multicolumn{@span}{c}{) suffix(})
				span erepeat(\cmidrule(lr){@span})
		)			
		;
	
#delimit cr
